<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="年度" prop="year">
        <el-select v-model="pram.year" placeholder="年度" clearable style="width: 100%">
          <el-option
              v-for="(item) in yearList"
              :key="item"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="departmentId">
        <el-select v-model="pram.departmentId" placeholder="所属单位" clearable style="width: 100%">
          <el-option
              v-for="(item,index) in departmentList"
              :key="index"
              :label="item.unitName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="本次引入资金额" prop="amount">
        <el-input v-model.number="pram.amount" type="number" placeholder="本次引入资金额，单位：万元" />
      </el-form-item>
      <el-form-item label="备注" prop="content">
        <el-input v-model="pram.content" type="textarea" :rows="7" placeholder="备注" />
      </el-form-item>
      <el-form-item label="完成日期" prop="finishTime">
        <el-date-picker v-model="pram.finishTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" placement="bottom-end" @change="onchangeTime" placeholder="完成日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:business:process:save','admin:business:process:info']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import  * as businessApi from '@/api/businessApi.js'
import * as roleApi from '@/api/role.js'
import {Debounce} from '@/utils/validate'
import {processAdd} from "@/api/businessApi.js";
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allDepartmentList:{
      type:Array
    },
    allYearList:{
      type:Array
    },
  },
  data() {
    const validateAmount = (rule, value, callback) => {
      let match = /(^[1-9]\d{0,5}$)|(^[1-9]\d{0,5}(\.\d{1,2}?)$)/
      if (!value) {
        return callback(new Error('请填写金额'));
      } else if (!match.test(value)) {
        ///^[1-0]\d{0,3}(\.\d{1,2}?)$/.test(str)||
        callback(new Error('金额最多不超过100亿，小数点后面最多两位!'));
      } else {
        callback();
      }
    };
    return {
      constants:this.$constants,
      pram: {
        year: null,
        departmentId:null,
        content:null,
        amount:null,
        finishTime:null,
        id: 0,
      },
      rules: {
        year: [{ required: true, message: '请选择年度', trigger: ['blur', 'change'] }],
        departmentId: [{ required: true, message: '请选择所属单位', trigger: ['blur', 'change'] }],
        content: [{ required: true, message: '请填工作任务', trigger: ['blur'] }],
        amount: [{ required: true, validator: validateAmount, trigger: ['blur', 'change'] }],
        finishTime: [{ required: true, message: '请选择完成时间', trigger: ['blur', 'change'] }],
      },
      departmentList:[],
      yearList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onchangeTime (e) {
      this.pram.finishTime = e;
    },

    initEditData() {
      this.yearList = this.allYearList
      this.departmentList = this.allDepartmentList

      if (this.isCreate !== 1) return //编辑
      const { departmentId, id, content, year, amount,finishTime } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.departmentId = departmentId
      this.pram.content = content
      this.pram.amount = amount
      this.pram.year = year
      this.pram.finishTime = finishTime
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) {
          return
        }
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      businessApi.processAdd(this.pram).then(data => {
        this.$message.success('创建招商引资任务完成情况成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      businessApi.processUpdate(this.pram).then(data => {
        this.$message.success('更新招商引资任务完成情况成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>
.tips{
  color:#999;
}
.btn-box{
  margin-top:20px;
}
</style>
